package com.mo.modular.tv.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mo.modular.auth.util.MacUtil;
import com.mo.modular.tv.dao.ZlSiteMapper;
import com.mo.modular.tv.dao.ZlUserMapper;
import com.mo.tv.model.ZlUser;
import com.mo.tv.service.IZlUserService;

/**
 * 账号密码验证
 *
 * @author chenxiaodong
 * @date 2018-08-23 12:34
 */
@Service
@Transactional
public class ZlUserServiceImpl implements IZlUserService {

	private static Logger logger = Logger.getLogger(ZlUserServiceImpl.class);
	
	@Resource
    ZlUserMapper zlUserMapper;
	
	@Autowired
	private ZlSiteMapper zlSiteMapper;
	
	@Autowired
    StringRedisTemplate stringRedisTemplate;


	@Override
	public Long insertTVInfo(ZlUser tvInfo) {
		zlUserMapper.insert(tvInfo);
		return tvInfo.getId();
	}
    @Override
    public boolean validate(ZlUser credence) {
        List<ZlUser> users = zlUserMapper.selectList(new EntityWrapper<ZlUser>().eq("tvid", credence.getTvid()));
        if (users != null && users.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    @Override
    public ZlUser findUser(String userName) {
        List<ZlUser> users = zlUserMapper.selectList(new EntityWrapper<ZlUser>().eq("tvid", userName));
        if (users != null && users.size() > 0) {
            return users.get(0);
        } else {
            return null;
        }
    }

	@Override
	public boolean checkAndAdd(String stbPrefix,String tvId) {
		try {
			String mac = tvId.substring(stbPrefix.length());
			if(MacUtil.isMac(mac)){
				ZlUser zlUser=new ZlUser();
				zlUser.setTvid(tvId);
				zlUser.setCreateTime(new Date());
				zlUserMapper.insert(zlUser);
				stringRedisTemplate.opsForValue().set(tvId, "tvid");
				return true;
			}else{
				logger.info("**********MAC地址无效**********");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
			return false;
		}
		
	}
}
